<template>
  <t-tooltip :content="help" :trigger="cpTooltipTrigger">
    <t-radio-group
      v-model="valueThis"
      :options="cpOptions"
      @change="change"
      v-bind="$attrs"
      class="custom-input-radio"
      :readonly="readonly"
      :disabled="disabled"
      :name="nameInnner"
    />
  </t-tooltip>
</template>

<script>

export default {
  desc: '自定义单选框',
  components: {
  },
  name: 'customRadio',
  props: {
    /**
     * @Description：上级页面对象
     * @Date 2023-10-10
     */
    parent: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：上级表单对象
     * @Date 2023-10-10
     */
    form: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：输入提示信息
     * @Date 2023-10-10
     */
    help: {
      type: String,
      default: () => ''
    },
    /**
     * @Description：只读
     * @Date 2023-10-10
     */
    readonly: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：禁用
     * @Date 2023-10-10
     */
    disabled: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：初始化默认值
     * @Date 2023-10-10
     */
    defaultValue: {
      type: String,
      default: () => null
    }
  },
  created() {
    this.bindFormItemProp()
  },
  data() {
    return {
      valueThis: '',
      nameInnner: global.utils.uuid(),

      // 表单配置
      formItemProp: {
        name: '',
        label: ''
      }
    }
  },
  methods: {
    // 值变化更新表单值
    change() {
      this.parent.$set(this.form, this.formItemProp.name, this.valueThis)
      this.$emit('change', this.valueThis)
    },
    /**
     * @Description：获取表单配置
     * @Author wyt
     * @Date 2023-09-27
     */
    bindFormItemProp() {
      if (!this.$parent.$options || !this.$parent.$options.propsData) {
        console.error('字段未找到表单项，请检查')
        return false
      }
      this.formItemProp = {
        name: this.$parent.$options.propsData.name,
        label: this.$parent.$options.propsData.label
      }

      if (!this.formItemProp.name) {
        console.error('字段未配置表单name')
        return false
      }
      if (!this.formItemProp.label) {
        console.error('字段未配置表单name')
        return false
      }
    }
  },
  watch: {
    // 监听表单设置本组件的值
    form: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.valueThis = ''
        if (this.defaultValue) {
          this.valueThis = this.defaultValue
        }
        if (this.form && this.form[this.formItemProp.name]) {
          this.valueThis = this.form[this.formItemProp.name]
        }
      },
    }
  },
  computed: {
    cpTooltipTrigger() {
      return this.strings.isBlank(this.help) ? '' : 'hover'
    },

    // 选项
    cpOptions() {
      let options = []
      if (this.$attrs && this.$attrs.options != undefined && this.$attrs.options.length > 0) {
        options = this.$attrs.options
      }
      return options
    }
  }
}
</script>

<style lang="scss">
</style>
